{{/* 参数说明：
    logo: 图片路径或者URL
    title: alt 文本
    class: CSS 类名
    config: 图片配置
*/}}

{{/* 日志时间 */}}
{{ $logTime := now.Format "2024-05-10 15:04:05" }}

{{/* 定义 logo 参数字典 */}}
{{ $logoParams := dict
    "width"  (.config.nav.width | default "auto")
    "height" (.config.nav.height | default 32)
    "title"  (.title | default "")
    "class"  (.class | default "lazyload")
}}

{{/* 主题名称 */}}
{{ $themeName := "WebStack-Hugo" }}
{{/* 其他信息 */}}
{{ $assetVersion := site.Params.assetVersion | default "v1" }}
{{ $logosPath := .config.nav.basePath | default "images/nav/" }}
{{ $logoName := .logo }}

{{/* 获取 logo 默认目录路径 */}}
{{ $defaultLogoPath := .config.nav.defaultLogo}}
{{ $defaultLogomatches := resources.Match $defaultLogoPath }}
{{ $defaultLogo := "" }}
{{ if $defaultLogomatches }}
    {{ $defaultLogo = index $defaultLogomatches 0 }}
{{ else }}
    {{/* 返回主项目默认第三方站点的 logo 图片 */}}
    {{ $defaultLogo = "images/nav/default/default.png" }}
{{ end }}

{{/*{{ partial "helpers/log" (dict "message" (printf "第三方站点 log 默认目录: %v" $defaultLogo)) }}*/}}

{{/* 1. 处理远程URL */}}
{{ if and $logoName (findRE "^(http|https)://" $logoName) }}
    <img src="{{ $logoName }}?v={{ $assetVersion }}"
        alt="{{ .title }}"
        class="{{ .class }}"
        loading="lazy">

{{/* 2. 处理本地资源 */}}
{{ else }}
    {{ $logoPath := printf "images/nav/%s" $logoName }}
    {{ $logMatches := resources.Match $logoPath }}
    {{ $resource := "" }}

    {{ if $logMatches }}
        {{/* 使用实际匹配到的资源路径 */}}
        {{ $resource = index $logMatches 0 }}
{{/*        {{ partial "helpers/log" (dict "message" (printf "[%s] ✅  通过 Match 获取资源: %s" $logoName $resource.RelPermalink)) }}*/}}
    {{ else }}
        {{/* 2. Match 失败时回退到精确路径查找 */}}
        {{ $themePath := printf "themes/%s/assets/images/nav/%s" $themeName $logoName }}
        {{ $mainPath := printf "assets/images/nav/%s" $logoName }}

        {{ partial "helpers/log" (dict "message" (printf "[%s] 🔍 查找主题资源: %s" $logoName $themePath)) }}
        {{ with resources.Get $themePath }}
            {{ $resource = . }}
            {{ partial "helpers/log" (dict "message" "[%s] ✅  主题资源加载成功" $logoName) }}
        {{ else }}
            {{ partial "helpers/log" (dict "message" (printf "[%s] 🔍 查找项目资源: %s" $logoName $mainPath)) }}
            {{ with resources.Get $mainPath }}
                {{ $resource = . }}
                {{ partial "helpers/log" (dict "message" "[%s] ✅  项目资源加载成功" $logoName) }}
            {{ else }}
                {{/* 3. 最终回退方案 */}}
                {{ $fallback := resources.Get "assets/images/default-logo.svg" }}
                {{ if $fallback }}
                    {{ $resource = $fallback }}
                    {{ partial "helpers/log" (dict "level" "WARN" "message" "[%s] ⚠️ 使用备用 Logo" $logoName) }}
                {{ end }}
            {{ end }}
        {{ end }}
    {{ end }}

    {{/* 最终资源输出 */}}
    {{ if $resource }}
{{/*        {{ partial "helpers/log" (dict "message" (printf "[%s] 🏁 最终资源路径: %s" $logoName $resource.RelPermalink)) }}*/}}
    {{ else }}
        {{ partial "helpers/log" (dict "level" "ERROR" "message" (printf "[%s] ❌  所有资源查找失败" $logoName)) }}
        {{ $resource = $defaultLogo | relURL }}
    {{ end }}

    {{/* 资源验证 */}}
    {{ if not (reflect.IsMap $resource) }}
        {{/* 将字符串转为资源对象，便于后续调用资源对象属性 */}}
        {{ $resource = resources.Get $resource }}
    {{ end }}

    {{ if $resource }}
        {{/* .RelPermalink 可使资源的显式引用，触发 Hugo 的资源发布机制 */}}
        <img src="{{ $resource.RelPermalink }}?v={{ $assetVersion }}"
            data-src="{{ $resource.RelPermalink }}?v={{ $assetVersion }}"
            width="{{ $logoParams.width }}"
            height="{{ $logoParams.height  }}"
            alt="{{ printf `%s的Logo` $logoParams.title }}"
            class="{{ $logoParams.class }}"
            loading="lazy"
            style="max-height: 100%; width: auto;"
            sizes="(max-width: 100px) 100vw, 50vw"
            onerror="{{ printf `this.src='%s?v=%s'` $defaultLogo $assetVersion | safeJS }}">
    {{ else }}
        <img src="{{ $defaultLogo }}?v={{ $assetVersion }}"
            data-src="{{ $defaultLogo }}?v={{ $assetVersion }}"
            width="{{ $logoParams.width }}"
            height="{{ $logoParams.height  }}"
            alt="{{ printf `%s的Logo` $logoParams.title }}"
            class="{{ $logoParams.class }}"
            loading="lazy"
            style="max-height: 100%; width: auto;"
            sizes="(max-width: 100px) 100vw, 50vw"
            onerror="{{ printf `this.src='%s?v=%s'` $defaultLogo $assetVersion | safeJS }}">
    {{ end }}
{{ end }}